<?php

namespace App\Admin\Actions\Image;

use App\Models\Admin\Image\ImageItem;
use App\Models\Image\Image;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class ImageAudit extends RowAction
{
    public $name = '审核';

    public function handle(Model $model, Request $request): Response
    {
        $uid = $request->get("uid");
        $remark = $request->get("audit_msg");
        $state = $request->get("is_show");
        $updateRow = Image::query()->where("uid", "=", $uid)->update([
            "audit_msg" => $remark,
            "is_show"   => $state,
        ]);
        ImageItem::query()->where("image_uid", "=", $uid)->update([
            "is_show" => $state,
        ]);
        if ($updateRow) {
            return $this->response()->success("审核成功")->refresh();
        }
        return $this->response()->error("审核失败");
    }

    public function form()
    {
        $audit = Image::query()->where("uid", "=", $this->getRow()->getAttribute("uid"))->first(["audit_msg", "is_show"])->toArray();
        $this->hidden("uid", "数据编号")->default($this->getRow()->getAttribute("uid"));
        $this->textarea("audit_msg", "审核描述")->default($audit["audit_msg"])->required();
        $this->radio("is_show", "审核结果")->options([1 => "通过", 2 => "驳回"])->default($audit["is_show"]);
    }
}
